<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/6/27
 * Time: 上午9:35
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'user';
    protected $fillable = [
        "id","parent_id","open_code","nickname","mobile","password","money","recharge_amount","pending_amount","is_disabled","app_platform","app_version","open_id","is_channel","link","client_ip","app_channel","osversion","device_code","ios_token","android_token","created_at","updated_at"
    ];

    protected $appends = ['app_platform_cn','app_channel_cn'];

    const PATH = "http://gskj.oss-cn-hangzhou.aliyuncs.com/20190720/7a92add316a7acd7912dd204d559cdea.png";

    public static function canUseMobile($mobile)
    {
        return self::where('mobile', $mobile)->count() ? false : true;
    }

    public static function findByCode($openCode)
    {
        return self::where('open_code', $openCode)->first();
    }

    public function oilCards()
    {
        return $this->hasMany(OilCard::class);
    }

    public function parent()
    {
        return $this->belongsTo(User::class, 'parent_id', 'id');
    }

    public function image(){
        return $this->belongsTo(Attachments::class, 'image_id', 'id');
    }

    public function getAppPlatformCnAttribute()
    {
        $name = '';
        switch($this->attributes['app_platform']){
            case 'ios':
                $name = '苹果';
                break;
            case 'android':
                $name = '安卓';
                break;
            case 'wechart':
                $name = '微信';
                break;
        }
        return $name;
    }

    public function getAppChannelCnAttribute()
    {
        $name = '';
        switch($this->attributes['app_channel']){
            case 'baidu':
                $name = '百度';
                break;
            case 'offical':
                $name = '官网';
                break;
            case 'huawei':
                $name = '华为';
                break;
            case 'xiaomi':
                $name = '小米';
                break;
            case '360':
                $name = '360';
                break;
            case 'oppo':
                $name = 'OPPO';
                break;
            case 'vivo':
                $name = 'VIVO';
                break;
            case 'yingyongbao':
                $name = '应用宝';
                break;
            case 'appstore':
                $name = 'AppStore';
                break;
            default:
                $name = '其他';
        }
        return $name;
    }

    public function coupons()
    {
        return $this->hasMany(UserCoupon::class)
            ->orderBy('status', 'asc')
            ->orderBy('finished_at', 'desc');
    }

    //生成opencode
    public static function randOpenCode(){
        //生成十位不重复的八位随机数
        $count = 0;
        $return_arr = array();
        while($count < 10){
            $return_arr[] = mt_rand(10000000,99999999);
            $return_arr = array_flip(array_flip($return_arr));
            $count = count($return_arr);
        }
        shuffle($return_arr);
        foreach ($return_arr as $item){
            $user = self::where("open_code",$item)->first();
            if (!$user){
                return $item;
            }
        }
    }

    public function getAvatarAttribute(){
        if ($this->image_id!='' || $this->image_id!=null){
            return $this->image->file_url;
        }else{
            return self::PATH;
        }
    }

    public function getNicknameAttribute(){
        if ($this->attributes['nickname']=='' || $this->attributes['nickname']==null){
            return substr_replace($this->attributes['mobile'],'****',3,4);
        }else{
            return $this->attributes['nickname'];
        }
    }

    public function getMobileAttribute(){
        if (isset($this->attributes['mobile']) && $this->attributes['mobile']=='(NULL)'){
            return null;
        }else{
            return isset($this->attributes['mobile'])? $this->attributes['mobile'] : '';
        }
    }
}